package com.qf.point.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.common.core.entity.PointRule;
import com.qf.common.redis.utils.RedisUtil;
import com.qf.point.mapper.PointRuleMapper;
import com.qf.point.service.PointRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zdl
 * @date 2025/3/25
 */
@Service
public class PointRuleServiceImpl implements PointRuleService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private PointRuleMapper pointRuleMapper;

    // 假如需要缓存数据库数据,流程：先查询缓存，缓存中有数据则直接返回，缓存中没有数据则查询数据库，查询到数据则存入缓存，返回数据。
    @Override
    public PointRule ruleInfo(Integer pointType) {
        PointRule pointRule = pointRuleMapper.selectOne(new LambdaQueryWrapper<PointRule>()
                .eq(PointRule::getPointType, pointType));
        return pointRule;
    }

    @Override
    public List<PointRule> ruleList() {
        // 查询缓存
        List<PointRule> list = redisUtil.getCacheObject("ponit:rule");
        if (list == null || list.isEmpty()) {
            // 查询数据库
            list = pointRuleMapper.selectList(null);
            // 使用redis   string存储json数据
            redisUtil.setCacheObject("ponit:rule", list, 60 * 60 * 24, TimeUnit.SECONDS);
        }
        return list;
    }
}
